﻿
###
### GAC the PS DPE
###
$assemblyFileName = '.\PowerShellDataProcessingExtension.dll'
if (-not (Test-Path $assemblyFileName))
{
    Throw "Unable to find the PowerShell Data Processing Extension DLL: $assemblyFileName"
}

$fullFileName = dir .\PowerShellDataProcessingExtension.dll | select -expand fullname
[Reflection.Assembly]::LoadWithPartialName("System.EnterpriseServices") | Out-Null
[System.EnterpriseServices.Internal.Publish] $publish = new-object System.EnterpriseServices.Internal.Publish
### 
### This method call will never return errors or exceptions
### On failure however it does put an entry into the event viewer
###
$publish.GacInstall($fullFileName)

###
### Get the Assembly (we use this to get the assembly FullName)
###
$psDpeAssembly = [reflection.assembly]::LoadWithPartialName("PowerShellDataProcessingExtension")
if (-not $psDpeAssembly)
{
   Throw "Failed to GAC the PowerShell DPE Assembly"
}

function Add-DpeToSsrsConfigurationFile
{
<#
.SYNOPSIS 
Finds and updates the config files for the SSRS Data Processing Extension

.DESCRIPTION
The Add-DpeToSsrsConfigurationFile function finds two configuration files:
1. RSReportServer.config   --> Config file for the SSRS Service
2. RSReportDesigner.config --> Config file for Business Intelligents Design Studio
It then constructs the XML entry once and injects it into both files.
   
.EXAMPLE
$conigFilePath = "c:\Program Files\Microsoft SQL Server\MSRS10_50.MSSQLSERVER\Reporting Services\ReportServer\rsreportserver.config"
$configXml = '<Extension Name="PowerShell" Type="PowerShellDataProcessingExtension.PSDBConnectionExtension, PowerShellDataProcessingExtension, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8003a7bdd48b0554" />'
Add-DpeToSsrsConfigurationFile -Path $conigFilePath -DpeConfigurationXmlString $configXml -Verbose

VERBOSE: Backing up C:\Program Files\Microsoft SQL Server\MSRS10_50.MSSQLSERVER\Reporting Services\ReportServer\RSReportServer.config ...
VERBOSE: Modifying C:\Program Files\Microsoft SQL Server\MSRS10_50.MSSQLSERVER\Reporting Services\ReportServer\RSReportServer.config ...
VERBOSE: DPE added to C:\Program Files\Microsoft SQL Server\MSRS10_50.MSSQLSERVER\Reporting Services\ReportServer\RSReportServer.config ...
	
DESCRIPTION
-----------
Updates the SSRS server conifiguration file, using two variables to store the config file path, and the config XML.

#>
   	param
    ( 
		<#
		A String containing the path to the config file
		#>
        [parameter(Mandatory=$true)]
        [ValidateScript({Test-Path $_})]
        $Path,
        <#
		The DPE Coniguration XML String
		#>
        [parameter(Mandatory=$true)]        
        $DpeConfigurationXmlString
    )###Closing param
    end
    { 
        [xml]$configXml = Get-Content $Path 
    	$rootReporting = $configXml.Configuration.Extensions.Data
    	#IF its Added Don't Add again
    	if($rootReporting.Extension | Where-Object {$_.Name -eq "PowerShell"})
        {		
    		Write-Verbose "PowerShell DPE already added to the $Path"
            Continue	
    	}
  
		### Backup the file before we modify it
		Write-Verbose "Backing up $Path ..."
		Copy-Item $Path ($Path + ".bak")		
	
		### Modify the SSRS RSReportConfig.config File
	    Write-Verbose "Modifying $Path ..."        
        $fraggle = $configXml.CreateDocumentFragment()
        $fraggle.InnerXML = $DpeConfigurationXmlString 
        
		[Void]$rootReporting.AppendChild($fraggle)
		$configXml.Save($Path);
        Write-Verbose "DPE added to $Path"
 
    }###Closing end
}###Closing function Add-DpeToSsrsConfigurationFile


###
### Find the SSRS path
###
if (Test-Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSRS10.MSSQLSERVER\Setup")
{
    $SSRSInstallFolder = (Get-ItemProperty -path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSRS10.MSSQLSERVER\Setup").SQLpath
}
elseif (Test-Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSRS10_50.MSSQLSERVER\Setup")
{
    $SSRSInstallFolder = (Get-ItemProperty -path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSRS10_50.MSSQLSERVER\Setup").SQLpath
}
elseif (Test-Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSRS11.MSSQLSERVER\Setup")
{
    $SSRSInstallFolder = (Get-ItemProperty -path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSRS11.MSSQLSERVER\Setup").SQLpath
}

###
### Construct the entries we want to put into the config files
###
$extensionConfigEntry = '<Extension Name="PowerShell" Type="PowerShellDataProcessingExtension.PSDBConnectionExtension, {0}" />' -F $psDpeAssembly.FullName

###
### Get the paths to the config files we need to modify
###
$SSRSInstallFolder 		= Join-Path $SSRSInstallFolder ReportServer
$RSReportServerConfig 	= Join-Path $SSRSInstallFolder rsreportserver.config
$RSSrvPolicyConfig    	= Join-Path $SSRSInstallFolder rssrvpolicy.config

$VSPrivateAssembliesFolder = "C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies" #TODO - discover this path instead of hard coding
$RSDesignerConfig 		= Join-Path $VSPrivateAssembliesFolder RSReportDesigner.config
$RSPreviewPolicy  		= Join-Path $VSPrivateAssembliesFolder RSPreviewPolicy.config

###
### Update the Config Files
###
Add-DpeToSsrsConfigurationFile -Path $RSReportServerConfig -DpeConfigurationXmlString $extensionConfigEntry -Verbose
Add-DpeToSsrsConfigurationFile -Path $RSDesignerConfig     -DpeConfigurationXmlString $extensionConfigEntry -Verbose

